package com.common.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * 日期工具类
 */
public class DateUtil {

    public static DateFormat DEFAULT_DATE_FORMAT=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd");
    public final static SimpleDateFormat SDF_YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
    public static final String SDF_YMDHMS = "yyyy-MM-dd HH:mm:ss";

    // 将10位时间戳1291778220 转为字符串 2010-12-08 11:17:00
    public static String long10ToString(long cc_time) {
        return  new SimpleDateFormat(SDF_YMDHMS).format(new Date(cc_time * 1000L));
    }

    public static Date parseDate(String str){
        return parseDate(str, null);
    }
    public static Date parseDate(String str,Date defaultDate){
        if(str==null||str.trim().length()==0){
            return defaultDate;
        }
        Date date=null;
        try {
            date=DEFAULT_DATE_FORMAT.parse(str);
        } catch (ParseException e) {
            date=defaultDate;
        }
        return date;
    }

    // 获取多少天之前
    public static String getYMDByDaysAgo(int interval) {
        Date date = new Date();
        long time = (date.getTime() / 1000) - interval * 60 * 60 * 24;
        date.setTime(time * 1000);
        try {
            return SDF.format(date);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return "";
    }


    // 获取多少天之前
    public static String getYMDByDaysAgo(String format,int interval) {
        Date date = parseSdf(format);
        long time = (date.getTime() / 1000) - interval * 60 * 60 * 24;
        date.setTime(time * 1000);
        try {
            return SDF.format(date);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return "";
    }

    public static Date parseSdf(String str) {
        try {
            Date parse = SDF.parse(str);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }
    /**
     * 获取时间字符串
     * @return
     */
    public static String getDate_yyyyMMdd(){
        //今天的时间
        return  DateFormatUtils.format(new Date(), "yyyy-MM-dd");
    }

    public static String getDate_yyyyMMddHHSS(){
        //今天的时间
        return  DateFormatUtils.format(new Date(), "yyyy-MM-dd mm:ss");
    }

    /**
     * 获取时间戳
     */
    public static Long getTimeByStr(String timeyyyymmdd) throws  Exception{
        return SDF.parse(timeyyyymmdd).getTime() / 1000;
    }

    /**
     * 格式化
     * @param s
     * @return
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    // 获取时间戳
    public static Long getLongByCurrTime() {
        return new Date().getTime() / 1000;
    }

    /**
     * 计算两个日期相差的天数
     * 2大 1小
     */
    public static long DaysBetweenByLong(Long date1, Long date2) {
        long day = (date2 - date1) / (60 * 60 * 24);
        return day;
    }


    // 得到当前月份的第一天 Author:lkx
    public static String getCurrentMonthFirstDay() {
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM");
        String strdate = s.format(new Date());
        // Calendar c = Calendar.getInstance();
        return strdate + "-01"; // + c.getActualMinimum(Calendar.DAY_OF_MONTH);
    }
    // 将10位时间戳1291778220 转为字符串 2010-12-08 11:17:00
    public static String long10ToSdf_hh_mm(Long cc_time) {
        long lcc_time = cc_time * 1000;
        return DEFAULT_DATE_FORMAT.format(new Date(lcc_time));
    }

    public static void main(String[] args) throws Exception {

        System.out.println(long10ToString(1643349662 ));
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");

        System.out.println(df.parse(df.format(strChnageDate(long10ToString(1643349662)))));
        System.out.println(strChnageDate(long10ToString(1643349662)));
        String s = long10ToSdf_hh_mm(1643338862L );
        System.out.println(s);
        System.out.println(s.substring(11,13));

        String beforeOneDay = getBeforeOneDay(new Date());
        System.out.println(beforeOneDay);

//        judgeIsSetBiddingTime();

//        System.out.println("parseDate:"+parseDate("2018-03-19 11:12:22"));
//        Date date=new Date();
//        System.out.println(DEFAULT_DATE_FORMAT.format(date));
//
//        System.out.println(new Date().getTime());
//        System.out.println(getLongByCurrTime());
//
//        getTimeByStr("2021-11-09");
//        System.out.println(DaysBetweenByLong(getTimeByStr("2021-11-05"), getLongByCurrTime()));

//        Long start = DateUtil.getLongByCurrTime();
//        Thread.sleep(5000);
//        Long end = DateUtil.getLongByCurrTime();
//        Long gap=end-start;
//        System.out.println(gap);
//
//        System.out.println(getYesWorkDay());

//        System.out.println(DEFAULT_DATE_FORMAT.format(new Date()));
//        System.out.println(getLongByCurrTime().toString().length());
    }

    public static String getMM_DD() {
        Date date = new Date();
        return  String.format("%tF",date).substring(5);
    }

    /**
     * 字符串转日期
     */
    public static  Date strChnageDate(String str){
        //创建SimpleDateFormat对象实例并定义好转换格式
        SimpleDateFormat sdf = new SimpleDateFormat(SDF_YMDHMS);
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return  date;
    }

    /**
     * 判断时间是否在9：00-15：00 范围内
     * @param theBegin 9:00
     * @param theEnd  15:00
     * @return
     */
    public static boolean judgeIsSetBiddingTime(String theBegin, String theEnd) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        Date now = null;
        Date beginTime = null;
        Date endTime = null;

        try {
            now = df.parse(df.format(new Date()));
            beginTime = df.parse(theBegin);
            endTime = df.parse(theEnd);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar date = Calendar.getInstance();
        date.setTime(now);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }


    /**
     * 判断时间是否在9：00-15：00 范围内
     * @param theBegin 9:00
     * @param theEnd  15:00
     * @return
     */
    public static boolean judgeIsSetBiddingTime2(String theBegin, String theEnd,Long time) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        Date now = null;
        Date beginTime = null;
        Date endTime = null;

        try {
            now =     df.parse(df.format(strChnageDate(long10ToString(time))));
            beginTime = df.parse(theBegin);
            endTime = df.parse(theEnd);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar date = Calendar.getInstance();
        date.setTime(now);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }


    /**
     * 获取当前系统时间的前一天时间
     * @return Date
     * @author xiaozhi
     * @date 2019.10.30
     * @version: 1.0
     * @return
     */
    public static String getBeforeOneDay(Date currentdate) {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentdate);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        currentdate = calendar.getTime();
        String dayTime=format.format(currentdate);
        return dayTime;
    }
    /**
     * 获取当前系统时间的前1秒时间
     * @return Date
     * @author xiaozhi
     * @date 2019.10.30
     * @version: 1.0
     * @return
     */
    public static String getOneSecondsTime(LocalDateTime localDateTime) {
//        DateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df= new SimpleDateFormat("yyyy-MM-dd");
        Date dates = Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());
        Calendar c = new GregorianCalendar();
        c.setTime(dates);//设置参数时间
        c.add(Calendar.SECOND,-1);//把日期往后增加SECOND 秒.整数往后推,负数往前移动
        dates=c.getTime(); //这个时间就是日期往后推一天的结果
        String secondsTime = df.format(dates);
        return secondsTime;
    }

    /**
     * 获取上一个工作日
     * yyyy-mm-dd
     */
    public static String getYesWorkDay(){
        String ymdByDaysAgo="";
        for (int i=1;i<10;i++){
            ymdByDaysAgo = DateUtil.getYMDByDaysAgo(i);
            Date date = parseSdf(ymdByDaysAgo);
            String httpArg = SDF_YYYYMMDD.format(date);
            String jsonResult = HolidayUtil.request(httpArg);
            if ("0".equals(jsonResult)) {
                break; //0上班
            }
        }
        return  ymdByDaysAgo;
    }

    /**
     * 获取上一个工作日
     * yyyy-mm-dd
     */
    public static String getYesWorkDay(String ymdByDaysAgo ){
        for (int i=1;i<10;i++){
            ymdByDaysAgo = DateUtil.getYMDByDaysAgo(i);
            Date date = parseSdf(ymdByDaysAgo);
            String httpArg = SDF_YYYYMMDD.format(date);
            String jsonResult = HolidayUtil.request(httpArg);
            if ("0".equals(jsonResult)) {
                break; //0上班
            }
        }
        return  ymdByDaysAgo;
    }

    /**
     * 比较两个日期 if date1<=date2 return true
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean compareDate(String date1, String date2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date d1 = format.parse(date1);
            Date d2 = format.parse(date2);
            return !d1.after(d2);
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

}